'use strict'

class user {

  constructor (mdb) {
    this.db = mdb
    this.tableName = 'users1'
  }

  makeId () {
    return `u_${Date.now().toString(16)}${parseInt(Math.random()*10000) + 11111}`
  }

  model () {
    return this.db.model(this.tableName)
  }
  
  makeDate() {  
    let date=new Date()
    console.log(date)
    var y = date.getFullYear();  
    var m = date.getMonth() + 1;  
    m = m < 10 ? '0' + m : m;  
    var d = date.getDate();  
    d = d < 10 ? ('0' + d) : d;  
    return y + '-' + m + '-' + d;  
};  

  async insert (data) {
    data.reg_time=this.makeDate()
    console.log(data)
    let r = await this.model().insert(data)
    return r.rowCount <= 0 ? false : data.id; 
  }
 
  async list(){
    let ulist =await this.model().limit(20).select()
    return ulist.rows
  }

  async get (userid) {
    let ulist = await this.model().where({userid}).limit(20).select()
    return ulist.rows
  }

  async delete(id){
    await this.model().where({userid:id}).delete();
  }

  async update(data,id){
    await this.model().where({userid:id}).update(data)
  }
}

module.exports = user
